// pages/home/homeFace/homeFace.js
const app = getApp();
const x = 10;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    btnStyle: ['#bf9368', '#3a3938', '#3a3938'],
    selectID:1,//选中的按钮ID

    scroImgs:[],//滚动图片
    actives:[],//最新动态
    varietys:[],//分类
    hots:[],//热门商家
    listData:[],//列表数据
    orderSData:[],//订单列表
    imgURL: app.globalData.imgURL,//图片总路径
   
    ordersStatu:"0",//查询订单状态

    pageNum: '1',//分页
    url:'buyerpurinfo/queryAllOrdersList?orderStatus=0',//分类请求url

    multipleTtemsNum : '5',
    noOrdersMsg: '未查询到订单信息~~',
    showViewId: '2',
    current:0,
  },

  //最新新闻点击
  newmessageClick:function(){
    console.log('最新新闻点击');
    wx.switchTab({
      url: '../../dynamic/dynamicList/dynamicList',
    })
  },
  newsListClick: function (e) {
    var that = this;
    console.log(e.currentTarget.dataset.url);
      wx.navigateTo({
        url: '../../news/newsDetail/newsDetail?url=' + e.currentTarget.dataset.url,
      })
  },
  hotShopClick:function(e){
    console.log(e.currentTarget.id);
    wx.navigateTo({
      url: '../shopMessage/shopMessage?id=' + e.currentTarget.id,
    })
  },

  phoneClick:function(e){
    console.log(e.currentTarget.id);
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.id,
    })
  },


  //分类按钮点击
  btnItemClick:function(e){
    console.log(e.currentTarget.id + "," + e.currentTarget.dataset.varid);
    var varid = e.currentTarget.dataset.varid;
    
    // wx.navigateTo({
    //   url: '../searchResult/searchResult',
    // })

    wx.navigateTo({
      url: '../materialClassificationNew/materialClassificationNew?tit=' + e.currentTarget.id + '&type=1&varid=' + varid,
    })
  },

  //搜索点击
  searchClick:function(){
    // wx.navigateTo({
    //   url: '../hSearch/hSearch',
    // })
    wx.navigateTo({
      url: '../materialClassification/materialClassification?search='+true,
    })
  },

  //商家详情
  shopMessage:function(e){
    var that = this;
    console.log(e);
    // if (that.data.selectID ==1){
    //   wx.navigateTo({
    //     url: '../productDetail/productDetail?id=' + e.currentTarget.id + '&openid=' + e.currentTarget.dataset.openid,
    //   })
    // }else{
    //   wx.navigateTo({
    //     url: '../shopMessage/shopMessage?id=' + e.currentTarget.id,
    //   })
    // }
  
  },

  //切换 未完成订单，已完成订单
  typeChoose:function(e){
    var that = this;
    console.log('切换 未完成订单，已完成订单');

    if (e.currentTarget.id == that.data.selectID){
      return;
    }
    console.log(e.currentTarget.id)
    if (e.currentTarget.id ==1){
      //未完成订单
      that.setData({
        btnStyle: ['#bf9368', '#3a3938', '#3a3938'],
        selectID: e.currentTarget.id,
        pageNum: '1',//分页
        // url: 'pro/appHomeList',//分类请求url
        url:'buyerpurinfo/queryAllOrdersList?orderStatus=0',
        listData: [],//列表数据
        orderSData: [],//列表数据
        current:0,
      })
    } else if (e.currentTarget.id==2){
      //优惠促销
      that.setData({
        btnStyle: ['#3a3938', '#bf9368', '#3a3938'],
        selectID: e.currentTarget.id,
        pageNum: '1',//分页
        url: 'buyerpurinfo/queryAllOrdersList?orderStatus=1',//分类请求url
        listData: [],//列表数据
        orderSData:[],//未完成订单列表
        current:0,
      })
    }else{
      //人气商家
      that.setData({
        btnStyle: ['#3a3938','#3a3938', '#bf9368'],
        selectID: e.currentTarget.id,
        pageNum: '1',//分页
        url: 'shop/hotShopList',//分类请求url
        listData: [],//列表数据
        orderSData:[]
      })
    }
    that.requestData();
  },
  listSupplement:function(){
    var that = this;
    if (that.data.orderSData.length != 0 && (that.data.orderSData.length <= that.data.multipleTtemsNum)){
        that.setData({
          orderSData: that.data.orderSData.concat(that.data.orderSData),
        })
      if (that.data.orderSData.length != 0 && (that.data.orderSData.length <= that.data.multipleTtemsNum)) {
        that.listSupplement();
      }
    }
  },

  //最新产品 优惠促销 人气商家 请求
  requestData:function(){
    var that = this;

    app.post(that.data.url, { pageNum: that.data.pageNum, multipleTtemsNum: that.data.multipleTtemsNum}).then((res) => {
      console.log('success');
      console.log(res);
      if (res.code == 0) {
        if (res.resultData.length>0){
          that.setData({
            orderSData: that.data.orderSData.concat(res.resultData),
          })
        }
        
      } else {
        wx.showModal({
          title: '温馨提示',
          content: res.msg,
          showCancel: false,
        })
      }
      that.listSupplement();
      console.log("订单数量："+that.data.orderSData.length);
      that.showView();
    }).catch((errMsg) => {
      console.log('fail');
      console.log(errMsg + '失败');
    })
  },

  showView:function(){
    var that = this;
    if(that.data.orderSData.length>0){
      that.setData({
        showViewId: 1,
      })
    }else{
      that.setData({
        showViewId: 2,
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    app.post('app/list', {}).then((res) => {
      console.log('success');
      console.log(res);
      if (res.code == 0) {
        that.setData({
          scroImgs: res.scroImgs,
          hots: res.hots,
          actives: res.actives,
          listData: res.produces,//列表数据
          orderSData: res.orders,
          varietys: res.varietys,
        });
        that.listSupplement();
        that.showView();
      } else {
        wx.showModal({
          title: '温馨提示',
          content: res.msg,
          showCancel: false,
        })
      }
    }).catch((errMsg) => {
      console.log('fail');
      console.log(errMsg + '失败');
    })

    console.log('失败');
    const x= 12;
    console.log(x);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      btnStyle: ['#bf9368', '#3a3938', '#3a3938'],
      selectID: 1,//选中的按钮ID
      scroImgs: [],//滚动图片
      actives: [],//最新动态
      varietys: [],//分类
      hots: [],//热门商家
      listData: [],//列表数据
      orderSData:[],
      pageNum: '1',//分页
      // url: 'pro/appHomeList',//分类请求url
      url: 'buyerpurinfo/queryAllOrdersList',
    })

    wx.showNavigationBarLoading();
    setTimeout(function () {
      that.onLoad();
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
    }, 2000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('加载更多');
    var that = this;
    that.data.pageNum++;
    console.log(that.data.pageNum);
    that.requestData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})